package game;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AudioPlayer {

    private static final String EQUIP_SOUND_PATH = "path/to/equip_sound.wav"; // 装备音效路径
    private static final String LEVEL_UP_SOUND_PATH = "path/to/level_up_sound.wav"; // 升级音效路径
    private static final String SKILL_LEARN_SOUND_PATH = "C:\\Users\\GeekGuru\\Downloads\\魔法钢琴旋律循环.wav"; // 学习技能音效路径
    private static final String SHOP_SOUND_PATH = "path/to/shop_sound.wav"; // 商店音效路径
    private static final String USE_SKILL_SOUND_PATH = "path/to/use_skill_sound.wav"; // 使用技能音效路径
    private static final String ITEM_USE_SOUND_PATH = "path/to/item_use_sound.wav"; // 使用物品音效路径
    private static final String VAMPIRISM_SOUND_PATH = "path/to/vampirism_sound.wav"; // 吸血鬼音效路径
    private static final String DODGE_SOUND_PATH = "path/to/dodge_sound.wav"; // 闪避音效路径
    private static final String CRITICAL_HIT_SOUND_PATH = "path/to/critical_hit_sound.wav"; // 暴击音效路径
    private static final String VICTORY_SOUND_PATH = "path/to/victory_sound.wav"; // 胜利音效路径
    private static final String DEFEAT_SOUND_PATH = "path/to/defeat_sound.wav"; // 失败音效路径
    private static Clip musicClip; // 用于背景音乐的Clip变量
    private static Clip effectClip; // 用于效果音的Clip变量

    // 重载playSound方法，增加loop参数控制是否循环播放，并区分背景音乐和效果音
    public static void playSound(String filePath, boolean loop, boolean isBackgroundMusic, float volume) {
        try {
            // 确保音频文件存在
            File audioFile = new File(filePath);
            if (!audioFile.exists()) {
                System.err.println("音频文件未找到：" + filePath);
                return;
            }

            // 准备音频输入流和格式
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
            AudioFormat baseFormat = audioInputStream.getFormat();
            AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16, // 确保16位深度
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2, // 正确的帧大小
                    baseFormat.getSampleRate(),
                    false // 小端序
            );

            // 解码音频输入流
            AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat, audioInputStream);

            // 选择适当的Clip对象
            Clip clip;
            if (isBackgroundMusic) {
                if (musicClip != null && musicClip.isOpen()) {
                    musicClip.stop();
                    musicClip.close();
                }
                musicClip = AudioSystem.getClip();
                clip = musicClip;
            } else {
                // 对于音效，始终使用一个新的Clip以避免冲突
                clip = AudioSystem.getClip();
            }

            // 打开并配置Clip
            clip.open(decodedAudioInputStream);
            if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
                FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(volume); // 调整音量
            }

            // 添加监听器，在播放完成时关闭Clip
            clip.addLineListener(event -> {
                if (event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                }
            });

            // 播放Clip
            if (loop) {
                clip.loop(Clip.LOOP_CONTINUOUSLY);
            } else {
                clip.start();
            }
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            System.err.println("播放声音失败：" + e.getMessage());
        }
    }

    public static void stopMusic() {
        if (musicClip != null) {
            musicClip.stop();
            musicClip.close();
        }
    }

    // 停止效果音
    public static void stopEffect() {
        if (effectClip != null) {
            effectClip.stop();
            effectClip.close();
        }
    }

    // 添加一个方法来调节音量

    // 修改后的setVolume方法，允许调整背景音乐或效果音的音量
    public static void setVolume(float volume, boolean isBackgroundMusic) {
        Clip targetClip = isBackgroundMusic ? musicClip : effectClip;
        if (targetClip != null && targetClip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
            FloatControl gainControl = (FloatControl) targetClip.getControl(FloatControl.Type.MASTER_GAIN);
            gainControl.setValue(volume); // 设置音量
        }
    }

    // AudioPlayer 类中添加新的播放音效方法

    public static void playSkillLearnSound() {
        // 停止背景音乐
        stopMusic();
        // 播放技能学习音效，不循环播放
        playSound("C:\\Users\\GeekGuru\\Downloads\\魔法钢琴旋律循环.wav", true, false, -10.0f);
    }
//    public static void playEquipSound() {
//        // 播放装备音效，不循环播放
//        playSound("path/to/equip_sound.wav", false, false);
//    }
//
//    public static void playLevelUpSound() {
//        // 播放升级音效，不循环播放
//        playSound("path/to/level_up_sound.wav", false, false);
//    }
//
//
//    public static void playShopSound() {
//        // 播放商店音效
//        playSound("path/to/shop_sound.wav", false, false);
//    }
//
//
//    // 在 AudioPlayer 类中添加新的播放音效方法
//    public static void playEquipSound() {
//        playSound(EQUIP_SOUND_PATH, false, false); // 播放装备音效
//    }
//
//    public static void playLevelUpSound() {
//        playSound(LEVEL_UP_SOUND_PATH, false, false); // 播放升级音效
//    }
//
//
//    public static void playShopSound() {
//        playSound(SHOP_SOUND_PATH, false, false); // 播放商店音效
//    }
//
//    public static void playUseSkillSound() {
//        playSound(USE_SKILL_SOUND_PATH, false, false); // 播放使用技能音效
//    }
//
//    public static void playItemUseSound() {
//        playSound(ITEM_USE_SOUND_PATH, false, false); // 播放使用物品音效
//    }
//
//    public static void playVampirismSound() {
//        playSound(VAMPIRISM_SOUND_PATH, false, false); // 播放吸血鬼音效
//    }
//
//    public static void playDodgeSound() {
//        playSound(DODGE_SOUND_PATH, false, false); // 播放闪避音效
//    }
//
//    public static void playCriticalHitSound() {
//        playSound(CRITICAL_HIT_SOUND_PATH, false, false); // 播放暴击音效
//    }
//
//    public static void playVictorySound() {
//        playSound(VICTORY_SOUND_PATH, false, false); // 播放胜利音效
//    }
//
//    public static void playDefeatSound() {
//        playSound(DEFEAT_SOUND_PATH, false, false); // 播放失败音效
//    }
}